package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.CurrStatusPeopleDao;
import cn.topevery.garden.entity.po.CurrStatusPeople;
import cn.topevery.garden.entity.query.CurrStatusPeopleQuery;
import cn.topevery.garden.entity.vo.CurrStatusPeopleVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.Value;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2021-09-01
 */
@Repository
public class CurrStatusPeopleDaoImpl extends BaseDaoImpl<CurrStatusPeople> implements CurrStatusPeopleDao {
    @Override
    public <P extends DbCsp> List<CurrStatusPeople> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<CurrStatusPeopleVO> listPage(CurrStatusPeopleQuery query) {
        String table = "t_curr_status_people tcsp " +
                "inner join t_person tp on tcsp.c_user_id = tp.c_user_id  " +
                "inner join t_sys_user tsu on tcsp.c_user_id=tsu.c_user_id "
               // "left join t_device_object_relate dr on dr.c_object_id= tp.c_id "
                ;

        String columns = "tcsp.*,tsu.c_user_mobile";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tcsp.c_online_type desc");
        sqlQueryMate
                .where(true, "tcsp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
             //   .where(true, "dr.c_object_type", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getName()), "tcsp.c_user_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotBlank(query.getDeviceCode()), "dr.c_code", ScriptConditionEnum.Like, query.getDeviceCode())
                .where(query.getExtentId() != null, "tp.c_extent_id", ScriptConditionEnum.Eq, query.getExtentId())
                .where(query.getOnlineType() != null, "tcsp.c_online_type", ScriptConditionEnum.Eq, query.getOnlineType())
                .where(query.getOnguardType() != null, "tcsp.c_onguard_type", ScriptConditionEnum.Eq, query.getOnguardType())
                .where(StringUtils.isNotBlank(query.getSectionId()),"tcsp.c_section_id",ScriptConditionEnum.Eq,query.getSectionId())
        ;


        return getDataPager(sqlQueryMate, pageSize, pageIndex, CurrStatusPeopleVO.class);
    }

    @Override
    public void currStatusRest(){
        run("p_job_curr_status_reset", CommandType.StoredProcedure, dbCommand -> {
            dbCommand.executeNonQuery();
        });
    }

    public Boolean uptOnlineStatus(){
        Value<Boolean> res = new Value<>();
        String sql = "UPDATE t_curr_status_people SET c_online_type=0\n" +
                "where c_db_status=0\n" +
                "and c_online_type=1\n" +
                "and c_gps_date<DATE_ADD(NOW(), INTERVAL -30 MINUTE)";
        run(sql, CommandType.Text, dbCmd -> {
            res.data = dbCmd.executeNonQuery();
        });
        return res.data;
    }

    @Override
    public Map getExtentWorkInfo(Integer extentId) {
        return null;
    }
}
